// pages/password/password.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 图片
    img_path: '',
    // 用户信息
      user_name: '',
      user_id: '',
      sex: '',
      role_name: '',
    isDisabled:true,
  },

  // 修改个人信息
  putt: function () {
    if(!this.data.isDisabled) {
      this.setData({
        isDisabled: true,
      })
    } else {
      this.setData({
        isDisabled: false
      })
    }
  },
  information: function () {
    const value = wx.getStorageSync('token');
    var that = this;
    if (this.data.user_name == '' || this.data.sex == '') {
      wx.showToast({
        title: '需填写全部填写框',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.baseurl+'/user/changeUser',
        method: 'put',
        data: {
          userName: that.data.user_name,
          sex: that.data.sex
        },
        header: {
          'content-type': "application/json",
          'Authorization':value,
        },
        success: function(res) {
          console.log(res);
          if (res.data.code == 200) {
            wx.showToast({
              title: '修改信息成功',
              icon: 'none',
              duration: 2000
            });
            wx.reLaunch({
              url: '../personal/personal',
            });
          } else if(res.data.code === 500) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.log('提交失败');
          wx.showToast({
            title: "提交失败！",
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */

  
  onLoad: function (options) {
    const value = wx.getStorageSync('token');
    var that = this;
    // 获取头像
    wx.request({
          url: app.globalData.baseurl+'/user/getUserImg',
          method:'get',
          header: {
            'content-type': "application/json",
			      'Authorization':value,
          },
          success (res) {
              console.log(res.data.data.img_path);
              that.setData({
                img_path: res.data.data.img_path
              })
          }
    });
    // 获取用户信息
    wx.request({
      url: app.globalData.baseurl+'/user/getUser',
      method:'get',
      header: {
        'content-type': "application/json",
        'Authorization':value,
      },
      success (res) {
          console.log(res.data);
          that.setData({
            user_name: res.data.data.user_name,
            user_id: res.data.data.user_id,
            sex: res.data.data.sex,
            role_name: res.data.data.role_name,
          })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})